import numpy as np
from PIL import Image
import tkinter as tk
from tkinter import filedialog



Filepath = ''
txtname = ''
root = tk.Tk()




frame1 = tk.Frame(root)
frame2 = tk.Frame(root)
root.title("文字绘画器")
label1 = tk.Label(frame1, text="绘画用的字符，用英文状态下的分号隔开\n目前仅支持两个字和三个字\n尽量把笔画少的字输在前面\n比如 又;墨;凝", justify=tk.LEFT)
label1.pack(side=tk.LEFT)
E1 = tk.Entry(frame1, bd =5)
E1.pack(side=tk.BOTTOM)
label2 = tk.Label(frame2, text="请选择图像")
label2.pack()
tip = tk.Label(frame2, text="")
tip.pack()


def openw():
    global Filepath
    global txtname
    Filepath = filedialog.askopenfilename() #获得选择好的文件
    print(Filepath)
    txtname = Filepath.replace(Filepath.split("/")[-1].split(".")[-1],"txt")
    print(txtname)
    label2["text"] = Filepath.split("/")[-1]

def ST():
    global Filepath
    global txtname
    str = ["一","二","三","宁","安","节","宏","宝","宦","害","寇","寒","寝","寨","寡","寮","寰","寲","寴","寵","寷"]
    str = str[::-1]
    print("------------",Filepath)

    im = Image.open(Filepath)
    if(im.width>300):
        p =300/int(im.width)
        im = im.resize((300, int(im.height*p)),Image.LANCZOS)

    print(im.width)
    print(im.height)
    image = im.convert('L')
    image = np.array(image)
    print(len(image))
    print(len(image[0]))
    if len(str) >0:
        with open(txtname, 'w') as f:
            for i in range(len(image)):
                for j in range(len(image[0])):
                    st = int(image[i][j] / 13)
                    f.write(str[st])
                    f.write(" ")
                f.write("\n")


select = tk.Button(frame2, text="选择文件", command=openw)
select.pack()
start = tk.Button(frame2, text="开始转换", command=ST)
start.pack()

frame1.pack(padx=1, pady=1)
frame2.pack(padx=10, pady=10)

root.mainloop()



